package com.wufun.overseas.utils;

import android.Manifest;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.util.Log;

import java.util.List;

/**
 * Created by lala on 14/12/22.
 */
public class SystemInfoUtils {
    static String TAG = SystemInfoUtils.class.getSimpleName();
    Context mContext;
    static SystemInfoUtils systemInfoUtils;

    private SystemInfoUtils(Context context) {
        mContext = context;
    }

    public static SystemInfoUtils getInstance(Context context) {
        if (systemInfoUtils == null)
            systemInfoUtils = new SystemInfoUtils(context);
        return systemInfoUtils;
    }

    /**
     * 获得手机屏幕高
     *
     * @return
     */
    public int getHeight(Activity activity) {
        DisplayMetrics dm = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
        return dm.heightPixels;
    }

    /**
     * 获得手机屏幕宽
     *
     * @return
     */
    public int getWidth(Activity activity) {
        DisplayMetrics dm = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
        return dm.widthPixels;
    } /**
     * 获得destiy
     *
     * @return
     */
    public float getDensity() {
        DisplayMetrics dm = new DisplayMetrics();
        dm = mContext.getApplicationContext().getResources().getDisplayMetrics();
        return dm.density;
    } /**
     * 获得destiy
     *
     * @return
     */
    public float getDensity(Context context) {
        DisplayMetrics dm = new DisplayMetrics();
        dm = context.getApplicationContext().getResources().getDisplayMetrics();
        return dm.density;
    }

    /**
     * 获取IMEI号，IESI号，手机型号
     */
    public String getInfo() {
        TelephonyManager mTm = (TelephonyManager) mContext
                .getSystemService(Context.TELEPHONY_SERVICE);

        String imei = "";
        try {
            imei = mTm.getDeviceId();
        } catch (Exception e) {
            e.printStackTrace();
        }
        String imsi = "";
        try {
            imsi = mTm.getSubscriberId();
        } catch (Exception e) {
            e.printStackTrace();
        }
        String mtype = Build.MODEL; // 手机型号
        String mtyb = Build.BRAND;// 手机品牌
        String numer = ""; // 手机号码，有的可得，有的不可得
        try {
            numer = mTm.getLine1Number();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "[version:" + getVersionName() + ",imei:" + imei + ",imsi:"
                + imsi + ",model:" + mtype + ",brand:" + mtyb + ",number:"
                + numer + "]";
    }

    public String getVersionName() {
        // 获取packagemanager的实例
        PackageManager packageManager = mContext.getPackageManager();
        // getPackageName()是你当前类的包名，0代表是获取版本信息
        PackageInfo packInfo = null;
        try {
            packInfo = packageManager.getPackageInfo(mContext.getPackageName(),
                    0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return "";
        }
        String version = packInfo.versionName;
        return version;
    }
    /**
     * device factory name, e.g: Samsung
     * 手机品牌
     * @return the vENDOR
     */
    public String getVendor() {
        return Build.BRAND;
    }
    public int getVersionCode() {
        // 获取packagemanager的实例
        PackageManager packageManager = mContext.getPackageManager();
        // getPackageName()是你当前类的包名，0代表是获取版本信息
        PackageInfo packInfo = null;
        try {
            packInfo = packageManager.getPackageInfo(mContext.getPackageName(),0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return 0;
        }
        int version = packInfo.versionCode;
        return version;
    }

    /**
     * versionCode versionName
     *
     * @return
     */
    public String[] getVersionNameArray() {
        String[] a = new String[2];
        // 获取packagemanager的实例
        PackageManager packageManager = mContext.getPackageManager();
        // getPackageName()是你当前类的包名，0代表是获取版本信息
        PackageInfo packInfo = null;
        try {
            packInfo = packageManager.getPackageInfo(mContext.getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return a;
        }
        a[0] = packInfo.versionCode + "";
        a[1] = packInfo.versionName;
        return a;
    }

    static String mac;

    /**
     * .获取手机MAC地址 只有手机开启wifi才能获取到mac地址
     */
    public String getMacAddress() {
            String result = "";
        try {
            if (StringUtils.isNotEmpty(mac)) return mac;
            WifiManager wifiManager = (WifiManager) mContext
                    .getSystemService(Context.WIFI_SERVICE);
            WifiInfo wifiInfo = wifiManager.getConnectionInfo();
            result = wifiInfo.getMacAddress();
            mac = result;
            Log.i("text", "phone mac Addr:" + result);
            if (mac == null || mac.equals("")) mac = getIDFinal();
        }catch (Exception e){
            if (mac == null || mac.equals("")) mac = getIDFinal();
        }
        return result;
    }

    public static String getIDFinal(){
        String m_szDevIDShort="35";
        try{
            m_szDevIDShort = "35" + //we make this look like a valid IMEI
                    Build.BOARD.length()%10 +
                    Build.BRAND.length()%10 +
                    Build.CPU_ABI.length()%10 +
                    Build.DEVICE.length()%10 +
                    Build.DISPLAY.length()%10 +
                    Build.HOST.length()%10 +
                    Build.ID.length()%10 +
                    Build.MANUFACTURER.length()%10 +
                    Build.MODEL.length()%10 +
                    Build.PRODUCT.length()%10 +
                    Build.TAGS.length()%10 +
                    Build.TYPE.length()%10 +
                    Build.USER.length()%10 ;
        }catch (Exception e){
            e.printStackTrace();
        }
        return m_szDevIDShort;
    }

    public void checkWifiStatePermission(Context context,int requestCode){
        int hasPermission = ContextCompat.checkSelfPermission(context,
                Manifest.permission.ACCESS_WIFI_STATE);
        if (hasPermission != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions((Activity) context,new String[]{Manifest.permission.ACCESS_WIFI_STATE},requestCode);
        }
    }

    /**
     *获取手机型号
     */
    public String getMobileMODEL(){
        return Build.MODEL;
    }
    /**
     *获取手机系统版本
     */
    public String getMobileSystenmVersion(){
        return Build.VERSION.RELEASE;
    }



    static String imei="";

    public String getDeviceId() {
        try{
            if (StringUtils.isEmpty(imei)) {
                TelephonyManager tm = (TelephonyManager) mContext
                        .getSystemService(Context.TELEPHONY_SERVICE);
                imei = tm.getDeviceId();
                if (StringUtils.isEmpty(imei)|| imei.equals("0"))
                    imei = getMacAddress().replaceAll(":", "_");
            }
        }catch (Exception e){
            imei = getMacAddress().replaceAll(":", "_");
        }
        return imei;
    }


    /**
     * 当前程序处于前台还是后台
     *
     * @param context
     * @return
     */
    public static boolean isBackground(Context context) {
        try {
            ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
            if (!tasks.isEmpty()) {
                ComponentName topActivity = tasks.get(0).topActivity;
                if (!topActivity.getPackageName().equals(context.getPackageName())) {
                    return true;
                }
            }
        }catch (Exception e){}

        return false;
      /*  ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
        for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
            if (appProcess.processName.equals(context.getPackageName())) {
                if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND) {
                    Log.i("后台", appProcess.processName);
                    return true;
                } else {
                    Log.i("前台", appProcess.processName);
                    return false;
                }
            }
        }
        return false;*/
    }


    public static boolean isSplash(Context context) {
        try{
            ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
            if (!tasks.isEmpty()) {
                ComponentName topActivity = tasks.get(0).topActivity;

                String topPackageName = topActivity.getPackageName();

                if (!topPackageName.equals(context.getPackageName())&&topActivity.getClassName().equals("com.join.mgps.activity_SplashActivity")) {
                    return true;
                }
            }
            return false;
        }catch (Exception e){}

        return false;
    }



}
